﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace Generator
{
    public class TabelaLL1
    {
        private List<string> Neterminale;

        private List<string> Terminale;

        private int[][] tabelaLL1;

        public TabelaLL1(List<string> Neterminale,List<string> Terminale,int[][] tabelaLL1)
        {
            this.Neterminale = Neterminale;
            this.Terminale = Terminale;
            this.tabelaLL1 = tabelaLL1;
        }

        public TabelaLL1(List<string> Neterminale, List<string> Terminale)
        {
            this.Neterminale = Neterminale;
            this.Terminale = Terminale;
            tabelaLL1=new int[Neterminale.Count][];
            for (int i = 0; i < Neterminale.Count; i++)
            {
                tabelaLL1[i] = new int[Terminale.Count];
                for (int j = 0; j < Terminale.Count; j++)
                    tabelaLL1[i][j] = -3;
            }
        }

        public void setRule(string Neterminal, string Terminal, int regula)
        {
            int i = -1, j = -1;

            foreach (var t in Terminale)
            {
                if (j == -1)
                    j = 0;
                if (t == Terminal)
                {
                    break;
                }
                j++;
            }

            foreach (var n in Neterminale)
            {
                if (i == -1)
                    i++;
                if (Neterminal == n)
                    break;
                i++;
            }

            if (i == -1 | j == -1)
                throw new Exception("Eroare la gasirea indexului.");

            tabelaLL1[i][j] = regula;
        }

        public int getRule(string Neterminal,string Terminal)
        {
            int i = -1, j = -1;

            foreach (var t in Terminale)
            {
                if (j == -1)
                    j = 0;
                if (t == Terminal)
                {
                    break;
                }
                j++;
            }

            foreach (var n in Neterminale)
            {
                if (i == -1)
                    i++;
                if (Neterminal == n)
                    break;
                i++;
            }

            if (i == -1 | j == -1)
                throw new Exception("Eroare la gasirea indexului.");

            return tabelaLL1[i][j];
        }

        public DataTable daTabelaLL1()
        {
            DataTable dt = new DataTable();
            DataColumn dc1 = new DataColumn();
            dc1.DataType = typeof(string);
            dc1.MaxLength = 100;
            dc1.ColumnName = " ";
            dt.Columns.Add(dc1);

            foreach (var t in Terminale)
            {
                DataColumn dc = new DataColumn();
                dc.DataType = typeof(int);
                dc.ColumnName = t;
                dt.Columns.Add(dc);
            }
            int i = 0;
            foreach (var n in Neterminale)
            {
                int j = 0;
                DataRow dr = dt.NewRow();
                foreach (var nc in dt.Columns)
                {
                    if (nc.ToString() == " ")
                        dr[nc.ToString()] = Neterminale[i];
                    else
                    {
                        if (tabelaLL1[i][j] != -3)
                            dr[nc.ToString()] = tabelaLL1[i][j];
                        j++;
                    }

                }
                dt.Rows.Add(dr);
                i++;
            }
            return dt;
        }

    }
}
